package node;

import lexer.LexType;
import parser.Parser;

import java.io.IOException;

public class PrimaryExpNode extends ASTNode {
    public final String name = "<PrimaryExp>";
    // PrimaryExp → '(' Exp ')' | LVal | Number | Character
    private ExpNode expNode;
    private LValNode lValNode;
    private NumberNode numberNode;
    private CharacterNode characterNode;

    public PrimaryExpNode(ExpNode expNode) {
        this.expNode = expNode;
    }

    public PrimaryExpNode(LValNode lValNode) {
        this.lValNode = lValNode;
    }

    public PrimaryExpNode(NumberNode numberNode) {
        this.numberNode = numberNode;
    }

    public PrimaryExpNode(CharacterNode characterNode) {
        this.characterNode = characterNode;
    }

    public LValNode getlValNode() {
        return lValNode;
    }

    public ExpNode getExpNode() {
        return expNode;
    }

    public int getNumber() {
        return numberNode.calculate();
    }

    public int getCharacter() {
        return characterNode.calculate();
    }

    public NumberNode getNumberNode() {
        return numberNode;
    }

    @Override
    public void show() throws IOException {
        if (expNode != null) {
            Parser.parserWriter.write(LexType.LPARENT + " " + "(" + "\n");
            expNode.show();
            Parser.parserWriter.write(LexType.RPARENT + " " + ")" + "\n");
        } else if (lValNode != null) {
            lValNode.show();
        } else if (numberNode != null) {
            numberNode.show();
        } else if (characterNode != null) {
            characterNode.show();
        }
        Parser.parserWriter.write(name + "\n");
    }

    public int calculate() {
        if (expNode != null) {
            return expNode.calculate();
        } else if (numberNode != null) {
            return numberNode.calculate();
        } else if (characterNode != null) {
            return characterNode.calculate();
        } else {
            return lValNode.calculate();
        }
    }

    public boolean canCaclulate() {
        if (expNode != null) {
            return expNode.canCaclulate();
        } else if (numberNode != null) {
            return true;
        } else if (characterNode != null) {
            return true;
        } else {
            return lValNode.canCaclulate();
        }
    }

}
